<?php


class CommentHelper extends AppHelper {
	
	var $helpers = array('Time', 'Cms.Image', 'Session');
	
	function show($options = array())
	{
	
		$defaultOptions = array(
			'title' => 'Comments',
			'element' => 'comments',
			'conditions' => array(),
			'limit' => false,
			'recursive'=> -1,
			'order'=>'created DESC',
			'cache' => '+2 minutes'
		);
		$options = am($defaultOptions, $options);
		if(!isset($options['model']))
		{
			trigger_error('Model name not provided while calling comments helper!!');
			exit;
		}
		extract($options);
				
		// @todo Add cache here
		$cacheKey = low($model.'_comments_cache');
		$comments = false;
		if($cache!==false)
		{
			$comments = gCache($cacheKey);
		}
		if($comments ===false)
		{
			$comments = cri($model)->find('all', compact('conditions', 'limit','recursive','fields','order'));
			if($cache!==false)
			{
				gCache($cacheKey,$comments,$cache);
			}
		}
		$View = ClassRegistry::getObject('view');
		return $View->element($element, am(array('comments'=>$comments,'plugin'=>'comments'), $options));
	}
	
	function showForm($type, $itemId, $model, $options= array())
	{
		$view = ClassRegistry::getObject('view');

		$formSettings = array();
		$formSettings['type'] = $type;
		$formSettings['item_id'] = $itemId;
		$formSettings['commentator'] = $type;
		
		if($this->Session->read('Auth.User'))
		{
			$loggedIn = true;
		}
		else 
		{
			$loggedIn = false;
		}

		return $view->element('comment_form', array('plugin'=>'comments','settings'=> $formSettings, 'loggedIn'=>$loggedIn,'model'=>$model,'options'=>$options));
	}
}
?>